package hehe;

import hehe.worker.WorkerExecutors;
import hehe.worker.base.Pool;
import hehe.worker.pool.WorkerPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by xfw on 17-6-12.
 */
public class ThreadMain {
    public static void main(String[] args) {


        worker1();
        // worker2();


//        MyThread mTh1=new MyThread("A");
//        MyThread mTh2=new MyThread("B");
//        mTh1.start();
//        mTh2.start();


//        MyThreadRunnable my = new MyThreadRunnable();
//        new Thread(my, "C").start();//同一个mt，但是在Thread中就不可以，如果用同一个实例化对象mt，就会出现异常
//        new Thread(my, "D").start();
//        new Thread(my, "E").start();

//        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
//        for (int i = 0; i < 10; i++) {
//            final int index = i;
//            try {
//                Thread.sleep(index * 1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            cachedThreadPool.execute(new Runnable() {
//                public void run() {
//                    System.out.println(index);
//                }
//            });
//        }

//        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
//        scheduledThreadPool.schedule(new Runnable() {
//            public void run() {
//                System.out.println("delay 3 seconds");
//            }
//        }, 3, TimeUnit.SECONDS);

//        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
//        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
//            public void run() {
//                System.out.println("delay 1 seconds, and excute every 3 seconds");
//            }
//        }, 1, 3, TimeUnit.SECONDS);
    }

    public static void worker1()
    {
                long workerTaskPooltime = System.currentTimeMillis();
        Pool workerTaskPool =  WorkerExecutors.newFixedWorkerPool(4);

        for (int i = 0;i < 1000000;i++) {
            workerTaskPool.pull(new Runnable(){
                public void  run()
                {
                    System.out.println("耗时:" + (System.currentTimeMillis() - workerTaskPooltime) + " ms");
                };
            });
        }
    }

    public static void worker2()
    {
        long starttime = System.currentTimeMillis();

        ExecutorService cachedThreadPool = Executors.newFixedThreadPool(4);
        for (int i = 0; i <1000000; i++) {
            cachedThreadPool.execute(new Runnable() {
                public void run() {
                    System.out.println("耗时:" + (System.currentTimeMillis() - starttime) + " ms");
                }
            });
        }
    }




}
